package com.company.Tree;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}14:53
 */
public class ArrBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        arrBinaryTree.postOrder(0);
        arrBinaryTree.preOrder(0);
    }

    static class ArrBinaryTree{
        private int[] arr;
        public ArrBinaryTree(int[] arr){
            this.arr = arr;
        }

        void preOrder(int index){
            if (arr == null || arr.length ==0){
                System.out.println("数组为空");
                return;
            }
            System.out.println(index);
            if ((index*2+1)<arr.length){
                preOrder(index*2+1);
            }

            if (index*2+2<arr.length){
                preOrder(index*2+2);
            }

        }

        public void postOrder(int index){
            if (arr == null || arr.length ==0){
                System.out.println("数组为空");
                return;
            }
            if ((index*2+1) < arr.length){
                postOrder(index*2+1);
            }
            if ((index*2+2) < arr.length){
                postOrder(index*2+2);
            }
            System.out.print(arr[index]);
        }
    }
}
